const Wechat = require('../util/Wechat'),
    log = require('../util/log'),
    logger = require('log4js').getLogger()

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, (reqObj, resolve) => {
        if (!reqObj.js_code) {
            resolve({
                result: -1,
                msg: '缺少js_code'
            })
            return
        }
        var wechat = Wechat({
            AppID: 'wx9725aecca23583dd',
            AppSecret: '22146690740cf653f38c1093ddbfdcb1'
        })
        wechat.gettoken().then(result => {
            wechat.snsJscode2session({
                js_code: reqObj.js_code
            }).then(result => {
                resolve({
                    openid: result.openid,
                    unionid: result.unionid
                })
            }, result => {
                logger.error('[' + __filename + ':' + __line + ']', result)
                resolve({
                    result: -1,
                    msg: result
                })
            })
        })
    })
}
